package com.hsy.sunflowerbao.home.ui

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.flux.action.RxStoreChange
import com.hsy.sunflowerbao.R
import com.hsy.sunflowerbao.base.action.ActionType
import com.hsy.sunflowerbao.base.ui.fragment.BaseFragment
import com.hsy.sunflowerbao.home.action.HomeActionCreator
import com.hsy.sunflowerbao.home.store.ListStore
import com.hsy.sunflowerbao.utils.CommonUtils
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshListener
import kotlinx.android.synthetic.main.layout_list_empty.*
import kotlinx.android.synthetic.main.layout_refresh_recycle.*

/**
 * 首页列表Fragment
 * Created on 2018/3/28.
 *
 * @author Yang
 */
class ListFragment : BaseFragment<ListStore>(), OnRefreshListener {

    private var failureView: View? = null
    /**
     * 事件产生器
     */
    private val creator = HomeActionCreator(dispatcher, manager)
    /**
     * 数据缓存器
     */
    override var store = ListStore(dispatcher)
    /**
     * 布局文件
     */
    override val fragmentLayout: Int
        get() = R.layout.layout_refresh_recycle

    /**
     * 问答分类
     */
    private lateinit var tab: String
    /**
     * 我的问答适配器
     */
    private val adapter = HomeAdapter(this, listOf())

    private var isCreate = false
    private var init = true

    override fun initView(view: View?) {
        tab = arguments!!.getString("tab")
        isCreate = true
        store.setRxTag(tab)
        refresh.setOnRefreshListener(this)
        refresh.isEnableLoadMore = false
        recycle.layoutManager = LinearLayoutManager(recycle.context)
        recycle.adapter = adapter
    }

    override fun initData() {
        super.initData()
        if (!init) {
            CommonUtils.showProgressDialog(context)
            onRefresh(refresh)
            init = true
        }
    }

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        if (isVisibleToUser) {
            init = isCreate
            if (isCreate) {
                CommonUtils.showProgressDialog(context)
                onRefresh(refresh)
            }
        }
    }

    /**
     * 重载刷新事件
     */
    override fun onRefresh(refresh: RefreshLayout) {
        store.page = "1"
        creator.getHomeData(tab, store.page, tab)
    }

    /**
     * 界面响应统一处理
     * @param change 需要界面响应的变化
     */
    override fun onRxStoreChanged(change: RxStoreChange) {
        if (change.storeId == store.tag) when (change.type) {
            ActionType.GET_HOME_DATA + tab -> {
                CommonUtils.hideProgressDialog()
                refresh.finishRefresh()
                if (change.success) {
                    if (failureView != null) {
                        homeParent.removeView(failureView)
                        failureView = null
                    }
                    adapter.items = store.homeData
                    adapter.notifyDataSetChanged()
                    listEmptyView.visibility = if (adapter.items.isNotEmpty()) View.GONE else View.VISIBLE
                } else {
                    failureView = context?.let { CommonUtils.showNetworkFailure(it, homeParent, View.OnClickListener { onFragmentRefresh() }) }
                }
            }
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        isCreate = false
    }

    override fun getRxTag(): String {
        return super.getRxTag() + tab
    }

    companion object {
        /**
         * 获取一个Fragment实例
         */
        fun getInstance(tab: String): ListFragment {
            val fragment = ListFragment()
            val argument = Bundle()
            argument.putString("tab", tab)
            fragment.arguments = argument
            return fragment
        }
    }
}